#include <Arduino.h>

#include <BLEDevice.h>  //Libraries necessary for program
#include <BLEUtils.h>
#include <BLEHIDDevice.h>
#include "keyscan.h"
#include "BleKeyboard.h"

BleKeyboard bleKeyboard;

#define   RGB_RED_ON      digitalWrite(12, 0)
#define   RGB_RED_OFF     digitalWrite(12, 1)
#define   RGB_GREEN_ON    digitalWrite(14, 0)
#define   RGB_GREEN_OFF   digitalWrite(14, 1)
#define   RGB_BLUE_ON     digitalWrite(27, 0)
#define   RGB_BLUE_OFF    digitalWrite(27, 1)


ITRButton button1 = {19, 0, false};
ITRButton button2 = {18, 0, false};

int flag = 0;  //标志�?
int flag_1 = 0;
boolean CW_1 = 0;
boolean CW_2 = 0;

NormalButton SW1;
NormalButton SW2;
NormalButton SW3;
NormalButton ENCODER_SW;

void key_trg(uint8_t pin)
{}

void key_cont(uint8_t pin)
{
  switch (pin)
  {
  case SW1_PIN:
    break;
  
  case SW2_PIN:
    break;

  case SW3_PIN:
    break;

  case ENCODER_SW_PIN:
    break;

  default:
    break;
  }
}

void key_cont_long(uint8_t pin)
{
  switch (pin)
  {
  case SW1_PIN:
    bleKeyboard.write(KEY_LEFT_ARROW);
    break;
  
  case SW2_PIN:
    break;

  case SW3_PIN:
    bleKeyboard.write(KEY_RIGHT_ARROW);
    break;

  case ENCODER_SW_PIN:
    break;

  default:
    break;
  }
}

void key_release(uint8_t pin)
{
  switch (pin)
  {
  case SW1_PIN:
    bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
    break;
  
  case SW2_PIN:
    bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);
    break;

  case SW3_PIN:
    bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
    break;

  case ENCODER_SW_PIN:
    bleKeyboard.write(KEY_MEDIA_MUTE);
    break;

  default:
    break;
  }
}

/*GPIO中断回调函数*/
void IRAM_ATTR isr() {
    // 只要处理一个脚的外部中�?--上升�?&下降�?
  int alv = digitalRead(19);
  int blv = digitalRead(18);
  if (flag == 0 && alv == LOW) {
    CW_1 = blv;
    flag = 1;
  }
  if (flag && alv) {
    CW_2 = !blv;  //取反是因�? alv,blv必然异步，一高一低�?
    if (CW_1 && CW_2) {
      button1.pressed = true;
      button1.numberKeyPresses += 1;
      flag_1 = 1;
    }
    if (CW_1 == false && CW_2 == false) {
      button1.pressed = true;
      button1.numberKeyPresses += 1;
      flag_1 = 2;
    }
    flag = 0;
  } 
}



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(button1.PIN, INPUT_PULLUP);
  attachInterrupt(button1.PIN, isr, CHANGE);
  pinMode(button2.PIN, INPUT_PULLUP);
  pinMode(23, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
  pinMode( 5, INPUT_PULLUP);
  pinMode(14, OUTPUT|PULLUP);
  pinMode(12, OUTPUT|PULLUP);
  pinMode(27, OUTPUT|PULLUP);
  delay(500);
  RGB_BLUE_OFF;
  RGB_RED_ON;
  RGB_GREEN_OFF;
  SW1.PIN = 22;
  SW2.PIN = 21;
  SW3.PIN = 5;
  ENCODER_SW.PIN = 23;
  delay(500);
  Serial.println("Starting BLE work!");
  // media_ble_setup(); //启动蓝牙
  bleKeyboard.begin();
}

void loop() {
  if(bleKeyboard.isConnected())
  {
    if (button1.pressed && flag_1 == 1)
    {
      Serial.printf("Button 1 has been pressed %u times\r\n", button1.numberKeyPresses);
      bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN);
      button1.pressed = false;
      flag_1 = 0;
    }
    if(button1.pressed && flag_1 == 2)
    {
      Serial.printf("Button 1 has been pressed %u times\r\n", button1.numberKeyPresses);
      bleKeyboard.write(KEY_MEDIA_VOLUME_UP);
      button1.pressed = false;
      flag_1 = 0;
    } 
    KeyScan(&ENCODER_SW);
    KeyScan(&SW1);
    KeyScan(&SW2);
    KeyScan(&SW3);
  }
}